tyNamespace.Components = cc.Class.extend({
    type: tyNamespace.ComponentType.Shun,
    tileType: null,
    tileTypeValue: 0,
    tile: null,
    components: [],
    singles: [],

    ctor: function () {
    	this.components = [];
    	this.singles = [];
    },
    
    addComponent: function(compnent) {
    	this.components.push(compnent);
    },
    
    getComponents: function (type) {
    	var result = [];
    	for (var i = 0; i < this.components.length; i++) {
    		var comp = this.components[i];
    		if (comp.type == type)
    			result.push(comp);
    	}

    	return result;
    },
    
    getComponentsTiles: function () {
    	var result = [];
    	for (var i = 0; i < this.components.length; i++) {
    		var comp = this.components[i];
    		
    		result = result.concat(comp.tiles);
    	}

    	return result;
    },
    
//    getBiggerComponents: function (type, num, length) {
//    	var result = [];
//    	
//    	if (type == tyNamespace.ComponentType.Dan) {
//    		var tile = tyNamespace.CardActions.getBiggerCard(this.singles, num);
//    		if (tile == null && num < 11)
//    			tile = tyNamespace.CardActions.getBiggerCard(this.getComponentsTiles(), 11);
//    		
//    		if (tile != null) {
//    			var tiles = [];
//    			tiles.push(tile);
//    			
//    			result.push(new tyNamespace.Component(tyNamespace.ComponentType.Dan, tiles));
//    		}
//    	} else {
//    		if (!length)
//    			length = 0;
//    		
//    		for (var i = 0; i < this.components.length; i++) {
//    			var comp = this.components[i];
//    			if (comp.type == type && comp.tiles[0].num > num && (comp.tiles.length >= length || type == tyNamespace.ComponentType.Zha)) {
//    				if (length > 0 && comp.tiles.length > length)
//    					comp.tiles = comp.tiles.slice(0, length);
//    				
//    				result.push(comp);
//    			}
//    		}
//    	}
//    	
//    	if (type != tyNamespace.ComponentType.Zha && result.length == 0)
//    		result = this.getComponents(tyNamespace.ComponentType.Zha);
//    	
//    	return result;
//    },
    
    getSmallerComponents: function (type) {
    	var result = [];
    	
    	if (type == tyNamespace.ComponentType.Dan) {
    		var tile = tyNamespace.CardActions.getSmallerCard(this.singles, num);
    		if (tile == null)
    			tile = tyNamespace.CardActions.getSmallerCard(this.getComponentsTiles(), num);

    		if (tile != null) {
    			var tiles = [];
    			tiles.push(tile);

    			result.push(new tyNamespace.Component(tyNamespace.ComponentType.Dan, tiles));
    		}
    	} else
	    	for (var i = 0; i < this.components.length; i++) {
	    		var comp = this.components[i];
	    		if (comp.type == type && comp.tiles[0].num < num)
	    			result.push(comp);
	    	}

    	return result;
    },
});